<?php

App::uses('AppController', 'Controller');
App::uses('MyEmail', 'Lib');

/**
 * CustomerContacts Controller
 *
 */
class CustomerContactsController extends AppController {

    /**
     * Scaffold
     *
     * @var mixed
     */
    public $scaffold;
    public $uses = array('WebSetting', 'CustomerContact');

    public function index() {
        //debug($this->WebSetting->getEmailContact());
        if ($this->request->is('post')) {
            $this->CustomerContact->set($this->request->data['CustomerContact']);
            if ($this->CustomerContact->validates()) {
                if ($this->CustomerContact->save()) {
                    $email = new MyEmail();
                    $emailTo = $this->WebSetting->find('first');
                    debug($emailTo);
                    $email->sendContact($emailTo['WebSetting']['email_notification'], $this->request->data['CustomerContact']);
                    $this->Session->setFlash('Thank you for Message.');
                    $this->redirect('/customer_contacts');
                }
            }
        }
    }
    
    public function view($id=NULL){
        if($id != NULL){
            $data = $this->CustomerContact->getAllDetails($id);
            $this->set('data',$data);
        }else{
            $this->redirect('/CustomerContacts/listAll');
        }
    }

    public function listAll() {
        //debug($this->CustomerContact->listAllContact());
        $this->set('all', $this->CustomerContact->listAllContact());
    }

    public function delete($id = NULL) {
        if ($id != NULL) {
            if ($this->CustomerContact->delete($id)) {
                $this->Session->setFlash('Remove Success!!');                
            }
        }
        $this->redirect('/CustomerContacts/listAll');
    }

}
